package com.example.loggingservice.service;

import com.example.loggingservice.entity.Log;
import com.example.loggingservice.mapper.LogMapper;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@RocketMQMessageListener(topic = "log-topic",consumerGroup = "log-consumer-group",
        maxReconsumeTimes = 8)
public class LogRocketMQListener implements RocketMQListener<Log> {
    Logger logger = LoggerFactory.getLogger(LogRocketMQListener.class);
    @Autowired
    private LogMapper logMapper;
    @Override
    public void onMessage(Log log) {
        try {
            logMapper.insert(log);
            logger.info("接收到日志" + log);
        } catch (Exception e) {
            //异常抛出后会重试消费，到达次数后进入死信队列
            throw new RuntimeException(e);
        }
    }
}
